home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8651 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.6 KB  |  105 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Passing Structures As Pointers; Easy Question I think....
  5. Date: 03 Mar 1996 19:43:27 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Mar3124327@qcd.lanl.gov>
  8. References: <4hcov3$h1d@newsbf02.news.aol.com>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: razine@aol.com's message of 3 Mar 1996 13:38:27 -0500
  13.  
  14. In article <4hcov3$h1d@newsbf02.news.aol.com>
  15. razine@aol.com (Razine) writes:
  16.  
  17. R: I recently ran into a problem passing a structure as a pointer to a
  18. R: function.  I then wanted to pass a indivdual variable to another function
  19. R: but it didnt work.  Here is a brief example.  Can anyone spot anything
  20. R: wrong.
  21. R: 
  22. R: typedef struct {
  23. R:     char name[81];
  24. R:     int address_num;
  25. R: } person_rec;
  26. R: 
  27. R: 
  28. R: 
  29. R: void main(void) {
  30.  
  31. main must be declared as returning int in a C program. The effect of
  32. returning void is undefined in the C standard.
  33.  
  34. R:    person_rec thisuser;
  35. R: 
  36. R:   display_person(&thisuser);
  37.  
  38. As display_person has not yet been declared, the compiler assumes it
  39. is compatible with the declaration `int display_person()'. Your actual
  40. definition actually returns void, so this assumption is wrong. As the
  41. compiler makes a wrong assumption, no one can tell you what happens
  42. from here on. (The compiler may document what happens, but it is
  43. certainly not required to.) declare `void
  44. display_person(person_rec*);' before trying to use it, or move the
  45. definition to before the use.
  46.  
  47. As display_person does not initialize the record before using it, you
  48. are using thisuser before initializing it. This is a disaster. I
  49. assume this is only because you were trying to make things, short and
  50. simple for us: please do not make things so short and simple that they
  51. actually become wrong!
  52.  
  53. R: 
  54. R: }
  55. R: 
  56. R: void display_address(int address) {
  57. R:     printf("Adress Number is %d\r\n",address);
  58.  
  59. Use of \r in printf is almost certainly wrong. The C library is
  60. supposed to put in the correct line terminator when you printf a \n. 
  61.  
  62. R:     return;
  63. R: }
  64. R: 
  65. R: void display_person(person_rec *pr) {
  66. R: 
  67. R:    printf("Persons Name : %s\r\n",pr->name);  /* another question why in
  68. R: this instance would I have to use the -> operand? */
  69.  
  70. The operator -> means that the left operand is a pointer to a
  71. struct. The . operator means that the left operand is a struct.
  72.  
  73. Remember %s assumes that the aaray of characters that you are sending
  74. in (loose usage) is terminated by a '\0'. If the array is not
  75. initialized, there is no guarantee what happens.
  76.  
  77. R:    display_address(pr->address_num);  
  78. R: 
  79. R: /* Basically I get an error on the line above, how would I just pass the
  80. R: interger value contained in pr->address_num */
  81.  
  82. The call looks correct. What error do you get?
  83.  
  84. R: 
  85. R:    return;
  86. R: }
  87. R: 
  88. R: 
  89. R: I hope someone can help, I found a work around but it is sloppy and I want
  90. R: to understand why, and what I am doing wrong.  Thanks
  91. R: 
  92.  
  93. Only if you show us a small complete compilable example with an
  94. explanation of _what_ error you get when trying to compile or run it.
  95.  
  96. Cheers
  97. Tanmoy
  98. --
  99. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  100. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  101. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  102. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  103. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  104. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  105.